package com.yaosheng.thread.lock;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Created by yaosheng on 2021/6/30.
 */
public class ReentrantLock1 {

    ReentrantLock lock = new ReentrantLock ();
    void m1(){
        for(int i=0;i<0;i++){
            try{
                lock.lock ();
                TimeUnit.SECONDS.sleep (1);
            }catch (InterruptedException e){
                e.printStackTrace ();
            }finally {
                lock.unlock ();
            }
            System.out.println (i);
        }
    }

    void m2(){
        try{
            lock.tryLock (5,TimeUnit.SECONDS);
            System.out.println ("m2...");
        } catch (InterruptedException e) {
            e.printStackTrace ();
        } finally {
            lock.unlock ();
        }
    }

    public static void main(String[] args) {

        ReentrantLock1 rl = new ReentrantLock1 ();
        new Thread (rl::m1).start ();
        try{
            TimeUnit.SECONDS.sleep (1);
        }catch (InterruptedException e){
            e.printStackTrace ();
        }
        new Thread (rl::m2).start ();
    }
}
